<?php

namespace app\admin\controller;

use app\admin\model\Menu;
use app\admin\model\MenuModel;
use app\admin\model\PowerModel;
use PHPUtil\UrlUtil;
use think\Db;
use think\Request;
use Wechat\TPWechat;

class Wechat extends Common
{

	public function index()
	{
		$menus = db('wechat_menu')->select();
		$data = [];
		$index = 0;
		foreach ($menus as $v)
		{
			if ($v['pid'] == 0)
			{
				$v['index'] = $index;
				$v['two_index'] = 0;
				$data[] = $v;
				$two_index = 1;
				foreach ($menus as $m)
				{
					if ($m['pid'] == $v['id'])
					{
						$m['index'] = $index;
						$m['two_index'] = $two_index;
						$data[] = $m;
						$two_index++;
					}
				}
				$index++;
			}
		}
		$this->assign('data', $data);
		return view();
	}

	// 保存菜单数据
	public function save_menu()
	{
		$name = input('param.name/a');
		$account = input('param.account/a');
		$value = input('param.value/a');
		// 启动事务
		Db::startTrans();
		try
		{
			// db
			db('wechat_menu')->where('id', '>', 0)->delete();
			foreach ($name as $key => $val)
			{
				$menu1 = ['name' => $val[0],'account' => $account[$key][0],'value' => $value[$key][0],'pid' => 0];

				$id = db('wechat_menu')->insertGetId($menu1);
				if (count($val) > 1)
				{
					$menu2 = [];
					foreach ($val as $k => $v)
					{
						if ($k == 0)
							continue;
						$menu2[] = ['name' => $v,'account' => $account[$key][$k],'value' => $value[$key][$k],'pid' => $id];
					}
					db('wechat_menu')->insertAll($menu2);
				}
			}
			// 提交事务
			Db::commit();
		}
		catch (\Exception $e)
		{
			// 回滚事务
			Db::rollback();
			return json(['code' => 0,'msg' => $e->getMessage()]);
		}
		return json(['code' => 200,'msg' => 'success']);
	}

	// 同步微信菜单
	public function sync_menu()
	{
		$wechatConfig = config('wechat');
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $wechatConfig['appid'] . '&secret=' . $wechatConfig['appsecret'];
		$ret = UrlUtil::sendGetRequest($url, 2);
		$wechat = json_decode($ret, true);
		$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $wechat['access_token'];

		$menus = db('wechat_menu')->select();
		$data = [];
		foreach ($menus as $v)
		{
			if ($v['pid'] == 0)
			{
				$menu['name'] = $v['name'];
				$menu['sub_button'] = [];
				foreach ($menus as $m)
				{
					if ($m['pid'] == $v['id'])
					{
						switch ($m['account'])
						{
							case 'view':
								$k = 'url';
								break;
							case 'click':
								$k = 'key';
								break;
							default:
								return json(['code' => 0,'msg' => '菜单类型异常']);
								break;
						}
						$menu['sub_button'][] = ['type' => $m['account'],'name' => $m['name'],$k => $m['value']];
					}
				}
				if (!$menu['sub_button'])
				{
					switch ($v['account'])
					{
						case 'view':
							$k = 'url';
							break;
						case 'click':
							$k = 'key';
							break;
						default:
							return json(['code' => 0,'msg' => '菜单类型异常']);
							break;
					}
					$menu['type'] = $v['account'];
					$menu[$k] = $v['value'];
				}
				$data['button'][] = $menu;
			}
		}
		$rs = UrlUtil::sendPostRequest($url, json_encode($data, JSON_UNESCAPED_UNICODE), 2, 2);
		$rs = json_decode($rs, true);
		if ($rs['errmsg'] == 'ok')
		{
			return json(['code' => 200,'msg' => '同步成功！']);
		}
		else
			return json(['code' => 0,'msg' => '同步失败！']);
	}

	public function text()
	{
		if (request()->isAjax())
		{
			$msg = input('param.msg');
			if ($msg)
			{
				$wechar_auto_reply = db('config')->where('name', 'wechar_auto_reply')->find();
				if ($wechar_auto_reply)
				{
					db('config')->where('name', 'wechar_auto_reply')->update(['value' => $msg]);
				}
				else
				{
					db('config')->insert(['name' => 'wechar_auto_reply','value' => $msg]);
				}
				update_config();
				return json(['code' => 200,'msg' => 'SUCCESS！']);
			}
			else
			{
				return json(['code' => 0,'msg' => '回复内容不能为空！']);
			}
		}
		else
		{
			$data = db('config')->where('name', 'wechar_auto_reply')->find();
			$this->assign('data', $data);
			return view();
		}
	}
}